﻿/*
   Copyright 2011 Andrew Davis

    This file is part of DeviousCreation.Base.

    DeviousCreation.Base is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DeviousCreation.Base is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DeviousCreation.Base.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.ComponentModel;

namespace DeviousCreation.Base.SQL
{
    [Serializable()]
    public abstract class BaseList<T> : List<T> where T : BaseClass
    {
        #region "Public Events"
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region "Protected Methods"
        protected void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
        protected void bc_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            objectStatus = ObjectStatus.isDirty;
            NotifyPropertyChanged("Item Changed");
        }
        #endregion

        #region "Variables"
        protected ObjectStatus objectStatus;
        #endregion

        #region "CRUD"
        public bool Update(string connStr)
        {
            bool result = true;
            switch (objectStatus)
            {
                case ObjectStatus.isNew:
                case ObjectStatus.isDirty:
                case ObjectStatus.isOld:
                    // CALL CREATE
                    foreach (T bc in this)
                    {
                        result = bc.Update(connStr);
                        if (!result)
                            return false;
                    }
                    break;
            }
            return true;
        }
        #endregion

        #region "Public Methods"
        public void AddEventHandler()
        {
            foreach(T bc in this)
                bc.PropertyChanged += new PropertyChangedEventHandler(bc_PropertyChanged);
        }
        public void AddNew(T bc)
        {
            this.Add(bc);
            objectStatus = ObjectStatus.isNew;
            NotifyPropertyChanged("Object Added");
        }
        #endregion


    }
}
